﻿#ifndef BIOTABBAR_H
#define BIOTABBAR_H

#include <QTabBar>
#include <QTabWidget>
#include <QPainter>
#include <QColor>
#include <QSize>

class BioTabBar : public QTabBar
{
    Q_OBJECT
public:
    BioTabBar(QWidget *parent) : QTabBar{parent}
    {

    }

    void setDir(bool dirV) {
        m_dirVertical = dirV;
    }
private:

signals:


protected:
    void paintEvent(QPaintEvent *event) override
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);

        for (int i = 0; i < count(); ++i) {
            QRect tabRect = this->tabRect(i);

            bool isSelected = (currentIndex() == i);

            /*绘制背景*/
            painter.save();
            painter.setBrush(Qt::white);           // 选中时使用白色文本
            painter.setPen(Qt::NoPen);
            if(0 == i) {
                painter.drawRoundedRect(tabRect.x(),tabRect.y(),20,20,15,15);
                painter.drawRect(tabRect.x()+12, tabRect.y(), tabRect.width()-12, 20);
                painter.drawRect(tabRect.x(), tabRect.y()+12, tabRect.width(), tabRect.height()-12);
            } else {
                painter.drawRect(tabRect);
            }
            painter.restore();

            if(!m_dirVertical){
                painter.save();
                QString tabText = this->tabText(i);
                QFont font = painter.font();
                font.setBold(true);
                if(isSelected) {
                    font.setPointSize(15);
                    painter.setPen(QColor("#000000"));
                } else {
                    font.setPointSize(14);
                    painter.setPen(QColor("#606266"));
                }
                painter.setFont(font);
                painter.drawText(tabRect, Qt::AlignCenter, tabText);
                painter.restore();

                painter.save();
                painter.setBrush(QColor("#409EFF"));
                painter.setPen(Qt::NoPen);
                if(isSelected) {
                    int linew = 30;
                    int x = (tabRect.width() - linew)/2 + tabRect.x();
                    int y = tabRect.y() + tabRect.height() - 13;

                    painter.drawRoundedRect(x,y,linew,5,3,3);
                }
                painter.restore();
            } else {

                if(isSelected) {
                    painter.save();
                    painter.setBrush(QColor("#CFEEFA"));
                    painter.setPen(Qt::NoPen);
                    painter.drawRect(tabRect.x()+5, tabRect.y()+12 ,tabRect.width()-5,tabRect.height()-24);
                    //painter.drawRoundedRect(tabRect.x()+5, tabRect.y()+10 ,tabRect.width()-5,tabRect.height()-20, 10, 10);
                    painter.restore();
                }


                painter.save();
                QString tabText = this->tabText(i);
                QFont font = painter.font();
                font.setBold(true);
                font.setPointSize(12);
                painter.setPen(QColor("#606266"));
                painter.setFont(font);
                painter.drawText(tabRect, Qt::AlignCenter, tabText);
                painter.restore();
            }
        }
    }

    QSize tabSizeHint(int index) const override{
        return QSize(120, 60);
    }

private:
    bool m_dirVertical = false;
};

class BioTabWidget : public QTabWidget {
    Q_OBJECT
public:
    explicit BioTabWidget(bool dirV,QWidget *parent = nullptr);

private:


signals:


private:
    BioTabBar* m_bar = nullptr;
};

#endif // BIOTABBAR_H
